package GUI;
import  authentication.*;
import java.awt.Panel;

import java.awt.*;
import javax.swing.*;

import sun.awt.image.ImageWatched.Link;

import java.awt.event.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
@SuppressWarnings("serial")
public class AdminLoginPanel extends Panel implements ActionListener{
	JButton login;
	JLabel welcomeLabel;
	JLabel emailLabel;
	JLabel passLabel;
	JTextField emailField;
	JPasswordField passField;
	JLabel feedback;
	//JLinkButton newUser;
	public AdminLoginPanel(){
		this.setBounds(0, 0, 800, 600);
		this.setLayout(null);

		welcomeLabel = new JLabel();
		welcomeLabel.setBounds(100, 50, 200, 20);
		welcomeLabel.setText("GUC E-Voting System Admin");
		//welcomeLabel.setFont()
		welcomeLabel.setLayout(null);
		this.add(welcomeLabel);
		
		emailLabel = new JLabel();
		emailLabel.setBounds(150, 140, 100, 20);
		emailLabel.setText("Email");
		emailLabel.setLayout(null);
		this.add(emailLabel);
		
		emailField = new JTextField();
		emailField.setBounds(250, 140, 200, 20);
		emailField.setLayout(null);
		this.add(emailField);
		
		passLabel = new JLabel();
		passLabel.setBounds(150, 190, 100, 20);
		passLabel.setText("Password");
		passLabel.setLayout(null);
		this.add(passLabel);
		
		passField = new JPasswordField();
		passField.setBounds(250, 190, 200, 20);
		passField.setLayout(null);
		this.add(passField);
		
		login = new JButton("Login");
		login.setBounds(250,240,80,30);
		login.setBackground(Color.white);
		login.addActionListener(this);
		this.add(login);
		
		feedback = new JLabel();
		feedback.setBounds(250, 100, 200, 20);
		feedback.setLayout(null);
		this.add(feedback);
		//login.setBounds(0, 0, 800, 800);
	}
	public boolean validateFields(){
		try{
			if(emailField.getText().equals("") || getPass(passField.getPassword()).equals("")){
				return false;
			}else{
				return true;
			}
		}	
		catch(NullPointerException e){
			return false;
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==login){
			AdminAuthentication auth = new AdminAuthentication();
			if(validateFields()){
				boolean feed = auth.adminLogin(emailField.getText(), getPass(passField.getPassword()));
				if(feed==false){
					feedback.setText("Wrong Email Password combination");
					repaint();
				}
				else{
					int id = auth.getAdminID(emailField.getText());
					if(id==-1){
						feedback.setText("Error contacting Server; please try again later");
					}
					else{
						MainFrame.getMainFrame().remove(this);
						MainFrame.getMainFrame().addViewVotingPnl(true);
					}
				}
			}else{
				feedback.setText("Must provide Email and Password");
				repaint();
			}
		}
	}

	public String getPass(char [] cc){
		try {  
	         //--- char-array with password  
	        // char[] cc = new char[]{ 'p','a','s','s','w','o','r','d'};  
	   
	         //--- default-encoding (or any other)  
	         Charset cs = Charset.defaultCharset();  
	         CharsetEncoder enc = cs.newEncoder();  
	        
	         //--- copy into CharBuffer  
	         CharBuffer cb = CharBuffer.allocate( cc.length);  
	         cb.put( cc);  
	         cb.flip();  
	   
	         //--- create byte-repraesentation  
	         ByteBuffer bb = enc.encode( cb);  
	         byte[] ba = bb.array();  
	           
	         //--- decode (just testing)  
	         return (new String( ba));  
	         //System.out.println( "decoded: " + testString);  
	      } catch ( Exception e ) {  
	         e.printStackTrace(); 
	         return null;
	      } 
	}
}


